<script type="text/javascript">
$(function(){
	var pg = App.base.fileVersionEdit.ui, pp = pg.params || {}
		, envs = Cache.base.env(App.code);

	var frm = {
		env: pg.seek('env'),
		ver: pg.seek('ver'),
		group: pg.seek('group'),
		path: pg.seek('path'),
		memo: pg.seek('memo'),
		file: pg.seek('file')
	}
	
	frm.env.mac('combo', {
		width: frm.ver.width() + 2,
		boxWidth: frm.ver.width() + 2,
		keyField: 'k',
		displayField: 'v',
		data: envs
	}).val(pp.env);
	
	function getValues(){
		return {
			appId: pp.appId,
			id: pp.id,
			fileId: pp.fileId,
			env: frm.env.val(),
			ver: frm.ver.val(),
			group: frm.group.val(),
			memo: frm.memo.val(),
			path: frm.path.val()
		};
	}
	
	function setValues(o){
		frm.env.val(o.env);
		frm.ver.val(o.ver);
		frm.group.val(o.group);
		frm.memo.val(o.memo);
		frm.path.val(o.path);
	}
	setValues(pp);
	
	function validate(p){
		var x = App.validate, v = App.base.validation,
			msg = pg.seek('msg').html('');
		return x(v.file.ver.env, p.env, msg, '环境')
			&& x(v.file.ver.group, p.group, msg, '分组')
			&& x(v.file.ver.ver, p.ver, msg, '版本号');
	}
	pg.find('input').blur(function(){
// 		var p = getValues();
// 		if (!validate(p))
// 			return;
	});
	pg.seek('ok').click(function(){
		var p = getValues();
// 		if (!validate(p)) {
// 			return;
// 		}
		//
		var fd = new FormData();
		for (var k in p) {
			fd.append(k, p[k] || '');
		}
		fd.append('file', frm.file[0].files[0]);
		$.ajax({
			url: App.ctx + "/file/version/save.do",
			type: 'POST',
			data: fd,
			// 必须false才会避开jQuery对 formdata 的默认处理 XMLHttpRequest会对 formdata 进行正确的处理
			processData: false,
			// 必须false才会自动加上正确的Content-Type
			contentType: false,
			success: function (rs) {
				var ro = mac.eval(rs);
				if (ro.success) {
					pg.callback(ro.data);//更新树	
					pg.close();
					mac.alert('提交成功');
				} else {
					pg.seek('msg').html(mac.msg(ro.data, ro.params));
				}
			},
			error: function (rs) {
				mac.alert('提交失败');
			}
		});
		return false;
	});
	
	pg.seek('close').click(function(){
		pg.close();
	});
});
</script>
<div class="left">
	<div class="form">
		<div class="item">
			<span class="label">环境：</span>
			<div class="combo fl" name="env"></div>
			<span class="msg">*</span>
			<div class="clear"></div>
		</div>
		<div class="item">
			<span class="label">版本号：</span>
			<input type="text" name="ver" placeholder="请填入版本号" />
			<span class="msg">*</span>
			<div class="clear"></div>
		</div>
		<div class="item">
			<span class="label">分组：</span>
			<input type="text" name="group" placeholder="请填入分组名" value="default" />
			<span class="msg">*</span>
			<div class="clear"></div>
		</div>
		<div class="item">
			<span class="label">目录或URL：</span>
			<input type="text" name="path" placeholder="目录或URL" />
			<div class="clear"></div>
		</div>
		<div class="item">
			<span class="label">文件上传：</span>
			<input type="file" name="file" />
		</div>
		<div class="item">
			<span class="label">备注：</span>
			<textarea name="memo" placeholder="请填入memo"></textarea>
			<div class="clear"></div>
		</div>
		<div class="item">
			<span class="label">&nbsp;</span>
			<div name="msg" class="msg"></div>
			<div class="clear"></div>
		</div>
		<div class="bottom">
			<button name="ok">确定</button>
			&nbsp;&nbsp;
			<button name="close">取消</button>
		</div>
	</div>
</div>